package com.attendancesystem.util;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// 添加连接池实现（示例使用HikariCP）
public class JDBCUtils {
//    public static void main(String[] args) {
//        try (Connection conn = JDBCUtils.getConnection()) {
//            System.out.println("连接成功: " + conn.getMetaData().getDatabaseProductVersion());
//        } catch (SQLException e) {
//            System.err.println("连接失败: " + e.getMessage());
//        }
//
//    }
    private static final HikariDataSource dataSource;

    static {
        try {
            HikariConfig config = new HikariConfig();
            config.setDriverClassName("com.mysql.cj.jdbc.Driver"); // 显式指定驱动
            config.setJdbcUrl("jdbc:mysql://localhost:3306/attendancesystem?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
            config.setUsername("root");
            config.setPassword("yyd922027");
            config.addDataSourceProperty("connectionTimeout", "30000"); // 添加超时配置
            dataSource = new HikariDataSource(config);
        } catch (Exception e) {
            throw new ExceptionInInitializerError("HikariCP 初始化失败: " + e.getMessage());
        }
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}

